package com.song.composejoke

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberImagePainter
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import com.song.composejoke.model.JokeModel
import com.song.composejoke.model.NewsModel
import com.song.composejoke.ui.item.JokeItem
import com.song.composejoke.ui.item.NewsItem
import com.song.composejoke.ui.page.MinePage
import com.song.composejoke.ui.theme.ComposeJokeTheme
import com.song.composejoke.ui.theme.Purple700

class MainActivity : ComponentActivity() {
    private var jokes = mutableStateListOf<JokeModel>()
    private var news = mutableStateListOf<NewsModel>()
    private var pageIndex = 0
    private var currentSelectTab = mutableStateOf(BottomType.Joke)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            ComposeJokeTheme {
                Surface(color = MaterialTheme.colors.background) {
                    Scaffold(topBar = {
                        TopBar()
                    }, bottomBar = {
                        BottomBar()
                    }) {
                        Content()
                    }
                }
            }
        }
        getData()
    }

    @Composable
    fun BottomBar() {
        BottomNavigation(
            modifier = Modifier.fillMaxWidth(),
            backgroundColor = MaterialTheme.colors.background,
            elevation = 12.dp
        ) {
            BottomType.values().forEach { bottomType ->
                BottomNavigationItem(
                    selected = currentSelectTab.component1().id == bottomType.id,
                    icon = {
                        Icon(
                            painterResource(bottomType.icon),
                            contentDescription = null,
                        )
                    },
                    label = {
                        Text(text = bottomType.dec)
                    },
                    alwaysShowLabel = true,
                    onClick = {
                        currentSelectTab.value = bottomType
                        Toast
                            .makeText(baseContext, "点击${bottomType.dec}TAB", Toast.LENGTH_SHORT)
                            .show()
                    },
                    selectedContentColor = colorResource(id = R.color.purple_700),
                    unselectedContentColor = Color.Gray,
                )
            }
        }
    }

    @Composable
    fun Content() {
        LazyColumn(modifier = Modifier.padding(bottom = 50.dp)) {
            if (currentSelectTab.component1() === BottomType.Mine)
                item {
                    MinePage()
                }
            if (currentSelectTab.component1() === BottomType.News) {
                items(news) { newsModel ->
                    NewsItem(newsModel, baseContext)
                }
                item { ButtonMore() }
            }
            if (currentSelectTab.component1() === BottomType.Joke) {
                items(jokes) { jokeModel ->
                    JokeItem(jokeModel.content, jokes.indexOf(jokeModel), context = baseContext)
                }
                item { ButtonMore() }
            }
        }
    }

    @Composable
    fun TopBar() {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(50.dp)
                .background(Purple700),
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Image(
                painterResource(id = currentSelectTab.component1().icon),
                contentDescription = "标题",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .height(24.dp)
                    .width(24.dp)
            )
            Text(
                text = currentSelectTab.component1().dec,
                textAlign = TextAlign.Center,
                style = TextStyle(
                    color = colorResource(id = R.color.white),
                    fontSize = 16.sp
                )
            )
        }
    }

    @Composable
    fun ButtonMore() {
        Box(
            modifier = Modifier
                .padding(bottom = 16.dp, top = 10.dp)
                .fillMaxWidth(),
            contentAlignment = Alignment.Center
        ) {
            Button(onClick = {
                getData()
            }) {
                Text(text = "点击加载更多")
            }
        }
    }

    private fun getData() {
        if (currentSelectTab.component1() === BottomType.Joke) {
            JokeModel.getData(pageIndex++) { list ->
                jokes.addAll(list)
            }
        } else if (currentSelectTab.component1() === BottomType.News) {
            NewsModel.getData(pageIndex++) { list ->
                news.addAll(list)
            }
        }
    }


    @Preview(showBackground = true)
    @Composable
    fun DefaultPreview() {
        ComposeJokeTheme {
            Column() {
                JokeItem(
                    "有一天晚上我俩一起吃西瓜，老大把西瓜籽很整洁的吐在了一张纸上，过了几天，我从教室回但宿舍看到老大在磕瓜子，我就问他：老大，你什么时候买的瓜子？老大说：刚晒好，说着抓了一把要递给我……",
                    0, context = baseContext
                )
                ButtonMore()
            }
        }
    }
}